Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
problem with ".match(/\w+/g).length" in react

Using .match(/\w+/g).length while counting the number of words, returns an error if space is given at beginning of a sentence. How do i make it error-free?

The error says "can't read the properties of null (reading 'length')"

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

match returns null when no matches were found. In your case, it is when there are no words (empty strings or just white spaces). In other cases, it is an array. You should check if the result of the match is null before checking the .length.

One way to overcome this error, suppose str is the string you are checking:

const matchStr = str.match(/\w+/g) || []; // The || [] will give you empty array instead of null when no matches were found
matchStr.length // now you can safely use .length property, because you will always have an array.
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this str.match(/(\w+)/g).length

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda